<?php

namespace App\Http\Controllers\Strator;

use App\Http\Controllers\Controller;
use App\Http\Requests\Strator\StoreStratorPermissionRequest;
use App\Http\Requests\Strator\UpdateStratorPermissionRequest;
use App\Models\Strator\StratorDepartment;
use App\Models\Strator\StratorPermission;
use App\Services\Strator\StratorPermissionService;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class StratorPermissionController extends Controller
{
    protected Request $request;
    protected Model $model;

    public function __construct(Request $request,StratorPermission $stratorPermission)
    {
        $this->request=$request;
        $this->model = $stratorPermission;
    }

    //
    public function allInTree(Request $request, StratorPermissionService $permissionService){
//        $loginStrator=$request->user();
        $ids=$permissionService->getSuperPermissionIds();
        $tree=$permissionService->getPermissionTreeByPermissinIds($ids);
        return $this->success($tree);
    }
    //
    public function ownInTree(Request $request, StratorPermissionService $permissionService){
        $loginStrator=$request->user();
        $tree=$permissionService->getAllPermissions($loginStrator,1);
        return $this->success($tree);
    }

    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $perPage=$this->request->per_page??10;
        $list=$this->model->modelSearch($this->request,$this->model)
            ->orderByDesc('id')
            ->paginate($perPage);
        return $this->success($list);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(StoreStratorPermissionRequest $request)
    {
        $data=$this->request->only($this->storeOnly());
        $this->model->fill($data);
        $this->model->save();
        return $this->success(['id'=>$this->model->id]);
    }
    //
    public function storeOnly():array{
        return [
            'pid',
            'permission_name',
            'module_name',
            'route',
            'sort',
            'icon',
            'type',
            'permission_action',
            'status',
        ];
    }

    /**
     * Display the specified resource.
     */
    public function show()
    {
        $model=$this->model->findOrFail($this->request->id);
        return $this->success($model);
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(StratorPermission $stratorPermission)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(UpdateStratorPermissionRequest $request)
    {
        $model=$this->model->findOrFail($this->request->id);
        $data=$this->request->except($this->updateExcept());
        $model->fill($data);
        $model->save();
        return $this->successMsg();
    }
    //
    public function updateExcept():array
    {
        return [
            'id',
        ];
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy()
    {
        $model=$this->model->findOrFail($this->request->id);
        $model->delete();
        return $this->successMsg('删除成功');
    }
}
